package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.Resource;
import cn.com.blueInfo.bpm.system.enums.ResourceType;
import cn.com.blueInfo.bpm.system.mapper.ResourceMapper;
import cn.com.blueInfo.bpm.system.repository.ResourceRepository;
import cn.com.blueInfo.bpm.system.request.ResourceDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资源表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class ResourceRepositoryImpl
        extends ServiceImpl<ResourceMapper, Resource>
        implements ResourceRepository, BaseRepository<Resource, ResourceDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增资源
     * @param resource 资源参数
     */
    @Override
    public Integer createResource(Resource resource) {
        return sqlExecuteResult(save(resource));
    }

    /**
     * 批量新增资源
     * @param resourceList 资源参数列表
     */
    @Override
    public Integer batchCreateResource(List<Resource> resourceList) {
        return sqlExecuteResult(saveBatch(resourceList), resourceList.size());
    }

    /**
     * 删除资源
     * @param resourceId 资源ID
     */
    @Override
    public Integer deleteResource(Long resourceId) {
        Resource resource = new Resource();
        resource.setId(resourceId);
        resource.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(resource));
    }

    /**
     * 批量删除资源
     * @param resourceIdList 资源ID列表
     */
    @Override
    public Integer batchDeleteResource(List<Long> resourceIdList) {
        List<Resource> resourceList = new ArrayList<>();
        for (Long resourceId : resourceIdList) {
            Resource resource = new Resource();
            resource.setId(resourceId);
            resource.setDeleted(BaseDeleted.DELETED);
            resourceList.add(resource);
        }
        return sqlExecuteResult(removeBatchByIds(resourceList), resourceList.size());
    }

    /**
     * 更新部分资源
     * @param resource 资源参数
     */
    @Override
    public Integer updateResourcePartial(Resource resource) {
        validateUpdateId(resource.getId());
        LambdaUpdateWrapper<Resource> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Resource::getId, resource.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部资源
     * @param resource 资源参数
     */
    @Override
    public Integer updateResource(Resource resource) {
        validateUpdateId(resource.getId());
        return sqlExecuteResult(updateById(resource));
    }

    /**
     * 查询一条资源
     * @param resourceId 资源ID
     */
    @Override
    public Resource getResourceById(Long resourceId) {
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resource::getId, resourceId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询资源
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public Page<Resource> getResourcePage(ResourceDTO.QueryDTO resourceQueryDTO) {
        Page<Resource> resourcePage = new Page<>(
                resourceQueryDTO.getPage(), resourceQueryDTO.getSize());
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, resourceQueryDTO);

        BaseDeleted deleted = resourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(resourcePage, queryWrapper);
        } else {
            return page(resourcePage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 资源 数量
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public Long getResourceCount(ResourceDTO.QueryDTO resourceQueryDTO) {
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, resourceQueryDTO);

        BaseDeleted deleted = resourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 资源
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public List<Resource> getResourceList(ResourceDTO.QueryDTO resourceQueryDTO) {
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, resourceQueryDTO);

        BaseDeleted deleted = resourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<Resource> queryWrapper, ResourceDTO.QueryDTO resourceQueryDTO) {

        String resourceName = resourceQueryDTO.getResourceName();
        queryWrapper.eq(StringUtils.isNotBlank(resourceName), Resource::getResourceName, resourceName);

        ResourceType resourceType = resourceQueryDTO.getResourceType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(resourceType), Resource::getResourceType, resourceType);

        String permissionKey = resourceQueryDTO.getPermissionKey();
        queryWrapper.eq(StringUtils.isNotBlank(permissionKey), Resource::getPermissionKey, permissionKey);

        Long menuId = resourceQueryDTO.getMenuId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(menuId), Resource::getMenuId, menuId);

        addQueryWrapper(queryWrapper, resourceQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<Resource, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                Resource::getOrderNumber,
                Resource::getTenantId,
                Resource::getApplicationId,
                Resource::getStatus,
                Resource::getDeleted,
                Resource::getDeleteUserId,
                Resource::getDeleteTime,
                Resource::getRemark,
                Resource::getCreateUserId,
                Resource::getCreateTime,
                Resource::getUpdateUserId,
                Resource::getUpdateTime,
                Resource::getExtend1,
                Resource::getExtend2,
                Resource::getExtend3);
    }

}
